package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.StrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.util.AjaxResult;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.ControllerUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author win
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private StrategyTemplateSearchService strategyTemplateSearchService;

    @RequestMapping("")
    public String index(Model model) {
        // commends
        List<StrategyCommend> commends = strategyCommendService.selectHotTop5();
        model.addAttribute("commends", commends);
        //hotCds 热门攻略排行
        List<StrategyStatisVO> strategyStatisVOS = strategyStatisVOService.getHotStrategyTop10();
        model.addAttribute("hotCds", strategyStatisVOS);
        List<StrategyStatisVO> list = strategyStatisVOService.getAbroadOrUnAbroadStrategyList();
        // unabroadCds 国内排行
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();
        // abroadCds 国外排行
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        for (StrategyStatisVO vo : list) {
            if (vo.isIsabroad()) {
                if (abroadCds.size() < 10) {
                    abroadCds.add(vo);
                }
            } else {
                if (unabroadCds.size() < 10) {
                    unabroadCds.add(vo);
                }
            }
        }
        model.addAttribute("abroadCds", abroadCds);
        model.addAttribute("unabroadCds", unabroadCds);
        // themeCds 主题排行
        List<Map<String, Object>> themeCds = strategyTemplateSearchService.queryThemeRankTop10();
        model.addAttribute("themeCds", themeCds);
        // themes 查询主题
        List<StatisVo> themes = strategyTemplateSearchService.queryThemes();
        model.addAttribute("themes", themes);
        //abroads 国外主题查询
        List<StatisVo> abroads = strategyTemplateSearchService.queryAbroadsThemes();
        model.addAttribute("abroads", abroads);
        //chinas 国内主题查询
        List<StatisVo> chinas = strategyTemplateSearchService.queryChinasThemes();
        model.addAttribute("chinas", chinas);
        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String queryDetailById(Long id, Model model,@UserParam UserInfo userInfo) {
        StrategyDetail strategyDetail = strategyDetailService.get(id);
        strategyDetail.setStrategyContent(strategyDetailService.getContentById(id));
        model.addAttribute("detail", strategyDetail);
        model.addAttribute("vo", strategyStatisVOService.getVOByStrategyId(id));
        /*List<Long> userIds = strategyStatisVOService.getFavorUserIdByStrategyId(id);
        model.addAttribute("userIds", userIds);*/
        model.addAttribute("strategyIds", strategyStatisVOService.queryUserFavorStrategy(userInfo.getId()));
        strategyStatisVOService.countViewnumUp(id, 1);
        strategyStatisVOService.addHotScore(id, 1);
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        //toasts
        ControllerUtil.setToastsToModel(destinationService, model, qo.getDestId());
        //tags
        List<StrategyTag> list = strategyTagService.list();
        model.addAttribute("tags", list);
        // pageInfo
        PageInfo<StrategyDetail> pageInfo = strategyDetailService.queryDetailForListByDestId(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @RequiredLogin
    @ResponseBody
    public Object commentAdd(StrategyComment detail, @UserParam UserInfo userInfo) {
        try {
            strategyCommentService.save(detail, userInfo);
            strategyStatisVOService.commentNumUp(detail.getDetailId(), 1);
            strategyStatisVOService.addHotScore(detail.getDetailId(), 1);
            return AjaxResult.SUCCESS.addData(strategyStatisVOService.getVOByStrategyId(detail.getDetailId()).getReplynum());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.FAIL;
        }
    }

    @RequestMapping("/comment")
    public Object comment(Model model, StrategyCommentQuery qo) {
        model.addAttribute("page", strategyCommentService.query(qo));
        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        strategyCommentService.commentThumbUp(toid, fromid);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = strategyStatisVOService.favornumUp(sid, userInfo.getId());
        // 对排行分数进行修改
        strategyStatisVOService.addAbroadOrUnAbroadStrategyScore(sid, ret ? 1 : -1);
        return new AjaxResult(ret).addData(strategyStatisVOService.getVOByStrategyId(sid));
    }

    /**
     * 攻略顶
     */
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "没有登录, 请登录后再顶");
            result.setCode(102);
            return result;
        }
        strategyStatisVOService.addAbroadOrUnAbroadStrategyScore(sid, 1);
        boolean ret = strategyStatisVOService.strategyThumbup(sid, userInfo.getId());
        return new AjaxResult(ret, null).addData(strategyStatisVOService.getVOByStrategyId(sid));
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo") StrategySearchQuery qo) {
        Page<StrategyTemplate> page = strategyTemplateSearchService.query(qo);
        model.addAttribute("page", page);
        return "strategy/searchPageTpl";
    }

    @RequestMapping("/share")
    @ResponseBody
    public AjaxResult share(@UserParam UserInfo userInfo, Long id) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "没有登录请先登录");
            result.setCode(102);
            return result;
        }
        try {
            strategyStatisVOService.addSharenumUp(id, userInfo.getId());
            return new AjaxResult(true, "").addData(strategyStatisVOService.getVOByStrategyId(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.FAIL;
        }
    }
}
